//base
import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
//less
import './index.less'
import { deepCopy } from '../../../util/lodashmin'
import { showToast } from '../../../crossplatform/apiservice/toast'

class RadioCheckboxWrap extends Component {
  componentWillMount() {
    //onLoad
  }

  componentDidMount() {
    //onready
  }

  componentDidShow() {
    //onshow
  }

  componentDidHide() {
    //onhide
  }

  componentWillUnmount() {
    //onUnload
  }

  componentWillReceiveProps(props) {}

  delay(time = 0) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, time)
    })
  }

  doChangeCurrent = index => {
    const { number, current } = this.props
    if (number != 1) {
      if (JSON.stringify(current).indexOf(index) != -1) {
        if (number != 'multy') {
          if (current.length >= number) {
            showToast({
              title: `不能超过${number}个`
            })
            let currentNew = []
            current.forEach(data => {
              if (data != index) {
                currentNew.push(data)
              }
            })
            if (currentNew.length >= 1) {
              return currentNew
            } else {
              return current
            }
          } else {
            return current
          }
        } else {
          let currentNew = []
          current.forEach(data => {
            if (data != index) {
              currentNew.push(data)
            }
          })
          if (currentNew.length >= 1) {
            return currentNew
          } else {
            return current
          }
        }
      } else {
        if (number != 'multy') {
          if (current.length >= number) {
            showToast({
              title: `不能超过${number}个`
            })
            let currentNew = deepCopy(current)
            currentNew.push(index)
            if (currentNew.length >= 1) {
              return currentNew
            } else {
              return current
            }
          } else {
            return current
          }
        } else {
          let currentNew = deepCopy(current)
          currentNew.push(index)
          if (currentNew.length >= 1) {
            return currentNew
          } else {
            return current
          }
        }
      }
    } else {
      return [index]
    }
  }

  onChange = index => {
    const current = this.doChangeCurrent(index)
    this.props.onChange && this.props.onChange({ current: current })
  }

  havein = index => {
    const { current } = this.props
    let currentstr = JSON.stringify(current)
    return currentstr.indexOf(index) != -1
  }

  render() {
    const { dataSource = [], title, number } = this.props
    return (
      <View className="RadioCheckboxWrap_id13628">
        <View className="worktitlels">
          <View className="lefttl">{title}</View>
          <View className="rightsubtl">
            {number == 'multy' ? '(可多选)' : number == 1 ? '单选' : `（最多可选${number}个）`}
          </View>
        </View>
        {dataSource.map((data, index) => {
          return (
            <View className="single" onClick={this.onChange.bind(this, index)}>
              <View className="sgleft">{data.name}</View>
              <View className={`sgright ${this.havein(index) ? 'sgrightact' : ''}`}>
                <View className="circle" />
              </View>
            </View>
          )
        })}
      </View>
    )
  }
}

export default RadioCheckboxWrap
